package com.hyg.lib_common.Base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.PixelFormat;
import android.os.Handler;

import androidx.appcompat.app.AlertDialog;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ProgressBar;

import com.hyg.lib_base.Net.NetworkOP;
import com.hyg.lib_base.base.MyBaseActivity;
import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.lib_common.DataUtil.UserSPUtils;
import com.hyg.lib_common.R;
import com.hyg.lib_common.SQLite.DatabaseManager;
import com.hyg.lib_common.constant.Constants;
import com.hyg.lib_common.sweetalertdialog.SweetAlertDialog;

import okhttp3.FormBody;

public class BaseActivity extends MyBaseActivity {
    View mTipView = null;
    protected boolean mCheckNetWork = true; //默认检查网络状态
    WindowManager mWindowManager;
    WindowManager.LayoutParams mLayoutParams;
    public SharedPreferences ssu = null, sick = null;
    public SharedPreferences sn = null, mc = null;
    public SharedPreferences.Editor editorn = null;
    public SQLiteDatabase db;
    public DatabaseManager mDatabaseManager;

    private int TextStates = 3; //字号
    //    private static final String TypeFace = "pingfangheiti.ttf";  //字体
    private int TypeStyle = 0;  //正常，斜体
    public SweetAlertDialog pDialog = null;

    public AlertDialog updateDialog = null;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //实现屏幕常亮，不暗屏
        setThemes();//设置字体风格
        initTipView();
    }

    public void setThemes() {
        /*   设置字体的字号   */
        ssu = getApplication().getSharedPreferences("SystemSetUp", Context.MODE_PRIVATE);   //系统设置
        sn = this.getSharedPreferences("HYG", Context.MODE_PRIVATE);          //慧中医 APP系统账号
        sick = this.getSharedPreferences("SickParent", Context.MODE_PRIVATE); // 体检的病人信息，单独存储
        mc = this.getSharedPreferences("Music", Context.MODE_PRIVATE);      //音乐的默认设置

        if (!Utils.isEmpty(ssu.getInt("TextSize", 1)))
            TextStates = ssu.getInt("TextSize", 1);
        if (0 == TextStates) {
            setTheme(R.style.Default_TextSize_Small);
        } else if (1 == TextStates) {
            setTheme(R.style.Default_TextSize_Middle);
        } else if (2 == TextStates) {
            setTheme(R.style.Default_TextSize_Big);
        } else /* if (3 == TextStates) */ {
            setTheme(R.style.Default_TextSize_Biggish);
        }

    }

//    public void check_version_update(final boolean IsShowText,boolean isSystemSetting) {
//        new PuGongYing().getUpdateVersion(this, IsShowText, isSystemSetting,new AppUpdateListener() {
//            @Override
//            public void onStart() {
//                AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
//                updateDialog = builder.create();
//                updateDialog=new MyDialog(BaseActivity.this,R.style.alert_dialog_light);
//                View dialogView = View.inflate(BaseActivity.this, R.layout.dialog_update_version, null);
//                TextView TvContent=dialogView.findViewById(R.id.content_tv);
//                TvContent.setText("应用更新中,请稍候...");
//                progressBar=dialogView.findViewById(R.id.progress_bar);
//                updateDialog.setView(dialogView);
//                updateDialog.show();
//            }
//
//            @Override
//            public void onProgress(int progress) {
//                if(progressBar!=null){
//                    progressBar.setProgress(progress);
//                }
//            }
//
//            @Override
//            public void onFail() {
//                if(updateDialog!=null && updateDialog.isShowing()){
//                    updateDialog.dismiss();
//                }
//
//            }
//
//            @Override
//            public void onSuccess() {
//                if(updateDialog!=null && updateDialog.isShowing()){
//                    updateDialog.dismiss();
//                }
//            }
//
//            @Override
//            public void onCancel() {
//                if(updateDialog!=null && updateDialog.isShowing()){
//                    updateDialog.dismiss();
//                }
//            }
//            @Override
//            public void onNoUpdateVersion() {
//
//            }
//        });
//    }


    public void ErrorDialog(String message) {
        Log.d("ErrorDialog", message);
        if (pDialog.isShowing() && !this.isFinishing())
            pDialog.dismiss();
        if (message.equals("服务器错误")){
            message = "服务器正忙，请稍后重试，或者联系客服人员";
        }
        if (message.equals("连接服务器超时，请稍后重试")){
            message = "连接超时，请您检查一下您的网络情况";
        }
        if (isFastClick())
            return;
        //        if (pDialog.isShowing())pDialog.dismiss();
        if (message.equals("请先登录")) {
            pDialog = new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE);
            pDialog.setTitleText("当前账号已经下线，请您重新登录");
            pDialog.setConfirmButton("我马上登录", new SweetAlertDialog.OnSweetClickListener() {
                public void onClick(SweetAlertDialog sweetAlertDialog) {
                    sweetAlertDialog.dismiss();

                    UserSPUtils.clearUserInfo(BaseActivity.this);
                    ToIntent(Constants.PATH_ACTIVITY_WELCOME, true);
                }
            });
        } else {
            pDialog = new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE);
            pDialog.setTitleText(message);
            pDialog.setConfirmButton("知道啦", new SweetAlertDialog.OnSweetClickListener() {
                public void onClick(SweetAlertDialog sweetAlertDialog) {
                    sweetAlertDialog.dismiss();
                }
            });
        }
        if (!pDialog.isShowing() && !this.isFinishing()) {
            pDialog.setCancelable(false);
            pDialog.show();
        }
    }



    // 与后台进行 数据请求
    public void sendPostNetwork(String str_tag, FormBody formBody, Handler myHandler, String sendUrl) {
        NetworkOP op = new NetworkOP(str_tag, formBody, myHandler, sendUrl);
        //使用类的对象op将三个实参发送，通过handler传递参数到NetworkOP中去，在NetworkOP将数据包装发送到规定的互联网地址
        op.okhttpAsyncPost();
    }


    // 与后台进行 数据请求
    public void sendGetNetwork(String str_tag, String string, Handler myHandler, String sendUrl) {
        NetworkOP op = new NetworkOP(str_tag, string, myHandler, sendUrl);
        //使用类的对象op将三个实参发送，通过handler传递参数到NetworkOP中去，在NetworkOP将数据包装发送到规定的互联网地址
        op.okhttpAsyncGet();
    }

    public void startActivityAnim() {
        overridePendingTransition(R.anim.fade, R.anim.hold);
    }

    @Override
    public void finish() {
        super.finish();
        //当提示View被动态添加后直接关闭页面会导致该View内存溢出，所以需要在finish时移除
        if (mTipView != null && mTipView.getParent() != null) {
            mWindowManager.removeView(mTipView);
        }
    }

    private void hasNetWork(boolean has) {
        if (isCheckNetWork()) {
            Log.d(TAG, "onReceive: 检查当前网络has " + has);
            if (has) {
                if (mTipView != null && mTipView.getParent() != null) {
                    mWindowManager.removeView(mTipView);
                }
            } else {
                if (mTipView.getParent() == null) {
//                    mWindowManager.addView(mTipView, mLayoutParams);
                }
            }
        }
    }

    private static long lastConnectTime;
    public static int unLock = 0; //通过连点 ，进行解锁

    public void setCheckNetWork(boolean checkNetWork) {
        mCheckNetWork = checkNetWork;
    }

    public boolean isCheckNetWork() {
        return mCheckNetWork;
    }

    private void initTipView() {
        /*  全局断网 网络状态改变 的提示和对话框     */
        LayoutInflater inflater = getLayoutInflater();
        mTipView = inflater.inflate(R.layout.layout_network_tip, null); //提示View布局
        mWindowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        mLayoutParams = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                PixelFormat.TRANSLUCENT);
        //使用非CENTER时，可以通过设置XY的值来改变View的位置
        mLayoutParams.gravity = Gravity.TOP;
        mLayoutParams.x = 0;
        mLayoutParams.y = 0;

        pDialog = new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE);
        //        pDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        pDialog.setTitleText("网络提示");
        pDialog.setContentText("您当前网络出现变化\n如出现异常，请检查下您的网络设置");
        pDialog.setConfirmButton("去设置", new SweetAlertDialog.OnSweetClickListener() {
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
                startActivity(new Intent("android.net.wifi.PICK_WIFI_NETWORK"));
            }
        });
        pDialog.setCancelButton("知道啦", new SweetAlertDialog.OnSweetClickListener() {
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
            }
        });
    }


    public int px2dip(float pxValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

      public int dp2px(int dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale);
    }





}
